using System;
using System.Web.UI;
using System.Web.Core;

namespace Forum.Business
{
    public class RoleAdd : ForumUserControl
    {
        protected ValuedTextBox subjectTextBox;
        protected ResourceButton saveButton;

        protected override void OnLoad(EventArgs e)
        {
            saveButton.Click += new EventHandler(SaveRole);
        }

        private void SaveRole(object sender, EventArgs e)
        {
            Role role = new Role();
            role.Name.Value = subjectTextBox.Value;
            role.RoleType.Value = GetRoleDefaultType();
            RoleManager.Create(role);

            Page.Response.Redirect(UrlManager.Instance.FormatUrl("role_list"));
        }

        private long GetRoleDefaultType()
        {
            long roleType = 0;
            roleType += Convert.ToInt64(((long)RoleType.AllowVisible).ToString(), 16);
            roleType += Convert.ToInt64(((long)RoleType.AllowDelete).ToString(), 16);
            roleType += Convert.ToInt64(((long)RoleType.AllowEditPermission).ToString(), 16);
            return roleType;
        }
    }
}